package com.party.service.document.impl;

import com.party.common.Page;
import com.party.mapper.DocumentMapper;
import com.party.model.Document;
import com.party.model.DocumentExample;
import com.party.service.document.DocumentService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-9-18
 * Time: 下午10:38
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class DocumentServiceImpl implements DocumentService {

    @Resource
    DocumentMapper documentMapper;

    @Override
    public int addDocument(Document document) {
        int num = documentMapper.insertSelective(document);
        return num;
    }

    @Override
    public int deleteDocumentByPrimaryKey(Integer id) {
        int num = documentMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += documentMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public Document getDocumentByPrimaryKey(Integer id) {
        Document document = documentMapper.selectByPrimaryKey(id);
        return document;
    }

    @Override
    public int updateDocumentByPrimaryKey(Document document) {
        int num = documentMapper.updateByPrimaryKeySelective(document);
        return num;
    }

    @Override
    public List<Document> getDocumentList(String type,Integer documentNum) {
        DocumentExample documentExample = new DocumentExample();
        DocumentExample.Criteria criteria = documentExample.createCriteria();
        Document document=new Document();
        //获取指定条数的数据
//        document.setType(type);
//        document.setDocumentNum(documentNum);
        criteria.andTypeEqualTo(type);
        documentExample.setOrderByClause("time desc");
        //获取指定的页码数据
        documentExample.setBeginNum(0);
        documentExample.setEndNum(documentNum);
        List<Document> documentList = documentMapper.getListPage(documentExample);
//        List<Document> documentList = documentMapper.getListPage(document);
        return documentList;
    }

    @Override
    public Page getDocumentList(Document document, int pageSize, int pageNumber) {
        DocumentExample documentExample = new DocumentExample();
        /**
         * 可以添加查询条件
         */
        if(document != null)
        {
            DocumentExample.Criteria criteria = documentExample.createCriteria();
            if(document.getTitle() != null && !"".equals(document.getTitle()))
            {
                criteria.andTitleLike("%"+document.getTitle()+"%");
            }
            if(document.getType() != null&& !"".equals(document.getType()))
            {
                criteria.andTypeEqualTo(document.getType());
            }
        }
        documentExample.setOrderByClause("time desc");

        //获取总的记录数量
        int count = documentMapper.countByExample(documentExample);
        //获取指定的页码数据
        documentExample.setBeginNum((pageNumber - 1) * pageSize);
        documentExample.setEndNum(pageSize);
        List<Document> list = documentMapper.getListPage(documentExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
